package br.com.dojo.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import br.com.dojo.bean.Servico;
import br.com.dojo.dao.ServicoDAO;

@Service
@Transactional
public class ServicoService {

	@Autowired
	ServicoDAO dao;

	public List<Servico> listarServicos() {

		List<br.com.dojo.entity.Servico> servicos = dao.findAll();

		List<Servico> result = new ArrayList<Servico>();
		for (br.com.dojo.entity.Servico servico : servicos) {
			Servico c = new Servico();
			c.setCodigo(servico.getCodigo());
			c.setDescricao(servico.getDescricao());
			c.setPreco(servico.getPreco().toString());
			c.setStatus(servico.getStatus());
			result.add(c);
		}

		return result;
	}
	
	public Servico getById(long codigo){
		br.com.dojo.entity.Servico servico = dao.getById(codigo);
		
		
		if(servico != null){
			Servico c = new Servico();
			c.setCodigo(servico.getCodigo());
			c.setDescricao(servico.getDescricao());
			c.setPreco(servico.getPreco().toString());
			c.setStatus(servico.getStatus());
			return c;
		}else{
			return null;
		}
	}

	@Transactional
	public void persist(Servico servico) {
		
		
		// TODO Auto-generated method stub
		br.com.dojo.entity.Servico c = new br.com.dojo.entity.Servico();
		
		//c.setCodigo(servico.getCodigo());
		c.setDescricao(servico.getDescricao());
		c.setPreco(Float.parseFloat(servico.getPreco()));
		c.setStatus(servico.isStatus());
		
		dao.save(c);
	}
	
	@Transactional
	public void remove(long codigo) {
		
		dao.delete(codigo);
	}

	public void merge(Servico servico) {
		// TODO Auto-generated method stub
				br.com.dojo.entity.Servico c = new br.com.dojo.entity.Servico();
				
				c.setCodigo(servico.getCodigo());
				c.setDescricao(servico.getDescricao());
				c.setPreco(Float.parseFloat(servico.getPreco()));
				c.setStatus(servico.isStatus());
				System.err.println(servico.getCodigo());
				dao.update(c);
		
	}

}
